package cn.diancan.action;

import java.io.File; 
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.Random; 
import javax.annotation.Resource;

import cn.diancan.config.Config;
import cn.diancan.exception.OutMsgException;
import cn.diancan.service.ImageFileAService;
import cn.diancan.util.CutImg;
import cn.diancan.util.FileHandle;
import cn.diancan.util.ResponseJSONUtil;
import cn.diancan.util.images.ImageSaveUtil;

public class YmUploadAction extends BaseAction{
	
	private File file;
	private Integer isCompre;//1压缩(默认的) 2不压缩(备用字段)
	private Integer isCutImage;//系统  1.需要切(默认的) 2.不需切的图片(小部分)
	private String fileContentType;
	private String fileFileName; 
	private String allowImageType="image/x-png,image/png,image/gif,image/pgif,image/jpeg,image/pjpeg,image/jpg,application/octet-stream; charset=ISO-8859-1,application/octet-stream,audio/mp3,video/mp4";//定义该Action允许上传的文件类型
	// 文件允许格式
	private String[] allowImageFiles = { ".gif", ".png", ".jpg", ".jpeg","file",".mp3",".mp4"  }; 
 
	
    public boolean checkImage(String type){
        String[] types=allowImageType.split(",");
        for(String s:types){
            if(s.equals(type)){
                return true;
            }
        }
        return false;
    } 
	/**
	 * 文件类型判断
	 * 
	 * @param fileName
	 * @return
	 */
	private boolean checkFileType(String fileName) {
		Iterator<String> type = Arrays.asList(this.allowImageFiles).iterator();
		while (type.hasNext()) {
			String ext = type.next();
			if (fileName.toLowerCase().endsWith(ext)) {
				return true;
			}
		}
		return false;
	}
    
    
	private ImageFileAService imageFileAService; 
	public ImageFileAService getImageFileAService() {
		return imageFileAService;
	}
	@Resource(name="imageFileAService")
	public void setImageFileAService(ImageFileAService imageFileAService) {
		this.imageFileAService = imageFileAService;
	}
	
	/**
	 * 可以裁切的图片
	 * @return
	 */
	public String image(){
		try {
		if(null==file){
				return ResponseJSONUtil.commonJson(0, "请选择上传图片");
		}
			//类型
	        if(!checkImage(fileContentType)){
	        	return ResponseJSONUtil.commonJson(0, "上传图片错误"); 
	        }
	        if(!checkFileType(fileFileName)){
	        	return ResponseJSONUtil.commonJson(0,"上传图片格式错误"); 
	        }
	        //大小
	        if(file.length() >= (1024*1024*3)){//不能大于3M
	        	return ResponseJSONUtil.commonJson( 0,"上传图片不能大于3M"); 
	        }
	        //需要验证
	        if(null != isCutImage && 2 ==isCutImage){ //不需要切图的
	            String filename=ImageSaveUtil.saveFile(file, fileFileName);
	            	
	        	return ResponseJSONUtil.commonJson(1, "上传成功", "{\"url\":\""+Config.dir.getAccesshost()+Config.dir.getDircode()+"/"+filename+"\"}"); 
	        	//return ResponseJSONUtil.toJson(  "{\"code\":\"1\",\"msg\":\"上传成功\",\"url\":\""+Config.dir.getAccesshost()+Config.dir.getDircode()+"/"+filename+"\"}"); 
	        } 
	        String url = imageFileAService.saveImage(file, fileFileName,CutImg.checkImageType(file.getPath()),isCompre);  
	    	
	        return ResponseJSONUtil.commonJson(1, "上传成功", "{\"url\":\""+url+"\"}"); 
	        //return ResponseJSONUtil.toJson( "{\"code\":\"1\",\"msg\":\"上传成功\",}"); 
		}catch(OutMsgException e1){
			return ResponseJSONUtil.commonJson(0, e1.getMessage() ); 
		}catch(Exception e) {
			System.out.println(e.getMessage());
		}
		return ResponseJSONUtil.commonJson(0, "请上传正确的图片"); 
	}
	
	/**
	 * 上传apk
	 * @return
	 */
	public String fileApk(){
		try {
			if(null==file){
				return ResponseJSONUtil.commonJson(0,  "请选择上传图片"); 
			}
			if (!fileFileName.toLowerCase().endsWith(".apk")){
				return ResponseJSONUtil.commonJson(0, "请选择上传正确文件"); 
			}
			 //大小
			if(file.length() >= (1024*1024*3)){//不能大于3M
				return ResponseJSONUtil.commonJson(0, "上传图片不能大于3M");
			}
			String today= (new SimpleDateFormat("yyyyMMdd")).format(new Date());
			String filename= "ycs_mobileapp/android_down/"+today+"/"+fileFileName;
			File newFile = new File(Config.dir.getRootpath()+"/"+filename);
        	if(!newFile.getParentFile().exists()){
        		newFile.getParentFile().mkdirs();
        	}
        	FileHandle.copyFile(file, newFile);  
        	return ResponseJSONUtil.textString("{\"code\":\"1\",\"msg\":\"上传成功\",\"url\":\""+Config.dir.getDownhost()+Config.dir.getDircode()+"/"+filename+"\"}"); 
		} catch (Exception e) { 
			System.out.println(e.getMessage());
		} 
		return ResponseJSONUtil.commonJson(0, "请上传正确的图片"); 
	}
	
	
	/**
	 * 音频
	 * @return
	 */
	public String soundFile(){
	    try {
		if(null==file){
			return ResponseJSONUtil.commonJson(0,  "请选择上传图片"); 
		}
		if (!fileFileName.toLowerCase().endsWith(".mp3") && !fileFileName.toLowerCase().endsWith("file")){
			return ResponseJSONUtil.commonJson(0, "请选择上传正确文件"); 
		}
		 //大小
		if(file.length() >= (1024*1024*3)){//不能大于3M
			return ResponseJSONUtil.commonJson(0, "上传文件不能大于3M");
		}
		/*int idxlast= fileFileName.lastIndexOf(".");
        	String lastName=".mp3";
        	if(-1!=idxlast){
        		lastName=fileFileName.substring(idxlast);
        	}*/
		
		String url = imageFileAService.saveSound(file, fileFileName, "mp3");
		return ResponseJSONUtil.commonJson(1, "上传成功", "{\"url\":\""+url+"\"}"); 
	    } catch (Exception e) { 
        	System.out.println(e.getMessage());
	    } 
	    return ResponseJSONUtil.commonJson(0, "请上传正确的图片"); 
	}
	
	/**
	 * 视频
	 * @return
	 */
	public String videoFile(){
	    try {
		if(null==file){
			return ResponseJSONUtil.commonJson(0,  "请选择上传图片"); 
		}
		if (!fileFileName.toLowerCase().endsWith(".mp4") && !fileFileName.toLowerCase().endsWith("file")){
			return ResponseJSONUtil.commonJson(0, "请选择上传正确文件"); 
		}
		 //大小
		if(file.length() >= (1024*1024*3)){//不能大于3M
			return ResponseJSONUtil.commonJson(0, "上传图片不能大于3M");
		}
		
		/*int idxlast= fileFileName.lastIndexOf(".");
        	String lastName=".mp4";
        	if(-1!=idxlast){
        		lastName=fileFileName.substring(idxlast);
        	}*/
		
        	String url = imageFileAService.saveVideo(file, fileFileName, "mp4");
        	return ResponseJSONUtil.commonJson(1, "上传成功", "{\"url\":\""+url+"\"}"); 
	    } catch (Exception e) { 
        		System.out.println(e.getMessage());
	    } 
	    return ResponseJSONUtil.commonJson(0, "请上传正确的图片"); 
	}
	
	
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	} 
	public String getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	public Integer getIsCutImage() {
		return isCutImage;
	}
	public void setIsCutImage(Integer isCutImage) {
		this.isCutImage = isCutImage;
	}
	public Integer getIsCompre() {
		return isCompre;
	}
	public void setIsCompre(Integer isCompre) {
		this.isCompre = isCompre;
	} 
	
	
	
}
